package com.cacshanghai.cac_oa_client.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cacshanghai.cac_oa_client.R;
import com.cacshanghai.cac_oa_client.beans.Leave;

import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by geyh on 2016/6/1.
 */
public abstract class LeaveListBaseFragment extends Fragment {

    private PtrClassicFrameLayout mPtrFrame;
    private LinearLayout ll_list;
    protected List<Leave> leaveList = getData();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View contentView = inflater.inflate(R.layout.fragment_leave_list, null);
        ll_list = (LinearLayout) contentView.findViewById(R.id.ll_list);

        mPtrFrame = (PtrClassicFrameLayout) contentView.findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refrashDate();
                        mPtrFrame.refreshComplete();
                    }
                }, 1400);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        mPtrFrame.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPtrFrame.autoRefresh();
            }
        }, 100);
        return contentView;
    }

    protected void refrashDate() {
        if (ll_list == null) {
            return;
        }
        ll_list.removeAllViews();
        for (final Leave leave : leaveList) {
            final View childView = LayoutInflater.from(getContext()).inflate(R.layout.item_leave, null);
            RelativeLayout rl_short = (RelativeLayout) childView.findViewById(R.id.rl_short);
            LinearLayout ll_detail = (LinearLayout) childView.findViewById(R.id.ll_detail);

            TextView tv_date = (TextView) childView.findViewById(R.id.tv_date);
            TextView tv_type = (TextView) childView.findViewById(R.id.tv_type);
            TextView tv_introduction = (TextView) childView.findViewById(R.id.tv_introduction);
            TextView tv_status = (TextView) childView.findViewById(R.id.tv_status);

            tv_date.setText(leave.getApplyDate() + "  " + leave.getStartEndTime());
            tv_type.setText(leave.getType());
            tv_introduction.setText(leave.getIntroduction());
            tv_status.setText(leave.getStatus());

            if (leave.isEmpty()) {
                rl_short.setVisibility(View.GONE);
                ll_detail.setVisibility(View.VISIBLE);
                TextView tv_submit = (TextView) childView.findViewById(R.id.tv_submit);
                tv_submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        leave.setApplyDate("2016-04-04");
                        leave.setType("事假");
                        leave.setIntroduction("请事假");
                        leave.setStartEndTime("09:00~18:00");
                        leave.setStatus("待审批");
                        refrashDate();
                    }
                });
            } else {
                childView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClick(childView);
                    }
                });
            }
            ll_list.addView(childView);
        }
    }

    private void onItemClick(View childView) {
        RelativeLayout rl_short = (RelativeLayout) childView.findViewById(R.id.rl_short);
        LinearLayout ll_detail = (LinearLayout) childView.findViewById(R.id.ll_detail);

        if (rl_short.getVisibility() == View.VISIBLE) {
            rl_short.setVisibility(View.GONE);
            ll_detail.setVisibility(View.VISIBLE);
        } else {
            rl_short.setVisibility(View.VISIBLE);
            ll_detail.setVisibility(View.GONE);
        }
    }

    protected abstract List<Leave> getData();

}
